/**
 * 湖南江河机电自动化设备股份有限公司 版权所有
 * Copyright @2016-2020 All Rights Reserved
 */
package com.hnjme.cnec.mdm.handler;

import com.hnjme.cnec.mdm.model.OriginOrganization;
import com.hnjme.cnec.mdm.model.OriginStaff;
import com.hnjme.cnec.mdm.service.MasterDataService;
import com.hnjme.cnec.mdm.ws.MdmDispatchManager;
import com.hnjme.cnec.mdm.ws.OrganizationResult;
import com.hnjme.cnec.mdm.ws.StaffResult;
import com.hnjme.cnec.mdm.ws.UpdateStateObj;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.event.Event;
import reactor.spring.annotation.Selector;

import java.util.ArrayList;
import java.util.List;

/**
 * @author outta
 * @Since 1.0 2018/8/29
 */
@Slf4j
@Component
public class MasterDataProcessor {
    @Autowired
    private MdmDispatchManager wsClient;

    @Autowired
    private MasterDataService masterDataService;

    @Selector(value = "organizationsProcess", reactor = "@processor")
    public void organizationsProcess(Event<List<OrganizationResult>> event) {
        log.info("==> \t处理从MDM接口收到机构信息！");
        List<OrganizationResult> organizationResults = event.getData();
        handleOrganization(organizationResults, false);
    }

    @Selector(value = "fiOrganizationsProcess", reactor = "@processor")
    public void fiOrganizationsProcess(Event<List<OrganizationResult>> event) {
        log.info("==> \t处理从MDM接口收到财务机构信息！");
        List<OrganizationResult> organizationResults = event.getData();
        handleOrganization(organizationResults, true);
    }

    /**
     * 处理机构的最终方法
     *
     * @param organizationResults 机构列表
     * @param isFinance           是否是财务机构
     */
    private void handleOrganization(List<OrganizationResult> organizationResults, Boolean isFinance) {
        if (null != organizationResults && organizationResults.size() > 0) {
            List<UpdateStateObj> updateList = new ArrayList<>(organizationResults.size());
            organizationResults.stream().forEach(wsOrg -> {
                if (masterDataService.processOrganization(OriginOrganization.convert(wsOrg), wsOrg.getOperType())) {
                    UpdateStateObj update = new UpdateStateObj();
                    update.setDisId(wsOrg.getDisId());
                    update.setSurveyType(isFinance ? "CNEC_NBDW_CW" : "CNEC_NBDW");
                    update.setState("2");
                    updateList.add(update);
                }
            });
            log.info("上报处理成功的机构，一共处理成功{}条记录", updateList.size());
            wsClient.updateStateBatch(updateList);
        }
    }


    @Selector(value = "staffsProcess", reactor = "@processor")
    public void staffsProcess(Event<List<StaffResult>> event) {
        log.info("==> \t处理从MDM接口收到员工信息！");

        List<StaffResult> staffResults = event.getData();
        if (null != staffResults && staffResults.size() > 0) {
            List<UpdateStateObj> updateList = new ArrayList<>(staffResults.size());
            staffResults.stream().forEach(wsStaff -> {
                if (masterDataService.processStaff(OriginStaff.convert(wsStaff), wsStaff.getOperType())) {
                    UpdateStateObj update = new UpdateStateObj();
                    update.setDisId(wsStaff.getDisId());
                    update.setSurveyType("CNEC_STAFF");
                    update.setState("2");
                    updateList.add(update);
                }
            });
            log.info("上报处理成功的员工，一共处理成功{}条记录", updateList.size());
            wsClient.updateStateBatch(updateList);
        }
    }
}
